import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useTokenStore = defineStore('accessToken', () => {
  const accesToken = ref<string>('')

  function getAccessToken() {
    return accesToken.value
  }

  function setAccessToken(token: string) {
    return new Promise((resolve) => {
      accesToken.value = token
      window.sessionStorage.setItem('access_token', token)
      resolve(true)
    })
  }

  function clearAccessToken() {
    return new Promise((resolve) => {
      accesToken.value = ''
      window.sessionStorage.removeItem('access_token')
      resolve(true)
    })
  }

  return { accesToken, getAccessToken, setAccessToken, clearAccessToken }
})
